<template>
  <van-cell class="comment-item">
    <van-image
      slot="icon"
      class="avatar"
      round
      fit="cover"
      :src="comment.aut_photo"
    />
    <div slot="title" class="title-wrap">
      <div class="user-name">{{ comment.aut_name }}</div>
      <van-button
        class="like-btn"
        :class="{ active: comment.is_liking }"
        :icon="comment.is_liking ? 'good-job' : 'good-job-o'"
        @click="onChangeLike"
      >
        {{ comment.like_count || '赞' }}
      </van-button>
    </div>

    <div slot="label">
      <p class="comment-content">{{ comment.content }}</p>
      <div class="bottom-info">
        <span class="comment-pubdate">{{ comment.pubdate | dateFormat }}</span>
        <van-button class="reply-btn" round @click="isShowReply">
          回复 {{ comment.reply_count }}
        </van-button>
      </div>
    </div>
  </van-cell>
</template>

<script>
import { addCommentLike, deleteCommentLike } from '@/api/commen.js'
export default {
  name: 'CommentItem',
  components: {},
  props: {
    comment: {
      type: Object,
      required: true
    }
  },
  data() {
    return {}
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  methods: {
    async onChangeLike() {
      // 如果 没有点赞 也就是false的时候 那么久 点击 请求点赞
      try {
        if (this.comment.is_liking) {
          // 那么 表示已经 点赞了 再点击一次 取消点赞
          await deleteCommentLike(this.comment.com_id)
        } else {
          await addCommentLike(this.comment.com_id)
        }
        this.comment.is_liking = !this.comment.is_liking
        this.comment.like_count = this.comment.is_liking
          ? ++this.comment.like_count
          : --this.comment.like_count
        this.$toast(this.comment.is_liking ? '喜欢你' : '讨厌你')
      } catch (err) {
        this.$toast.fail('点赞失败，请稍后重试')
      }
    },
    // 点击回复用户评论的时候 修改弹出层的状态
    isShowReply() {
      this.$parent.$parent.$parent.isReplyShow = true
      // console.log(this.comment)
      this.$emit('click_comment', this.comment)
    }
  }
}
</script>

<style scoped lang="less">
.comment-item {
  .avatar {
    width: 72px;
    height: 72px;
    margin-right: 25px;
  }
  .title-wrap {
    display: flex;
    justify-content: space-between;
    align-items: center;
    .user-name {
      color: #406599;
      font-size: 26px;
    }
  }
  .comment-content {
    font-size: 32px;
    color: #222222;
    word-break: break-all;
    text-align: justify;
  }
  .comment-pubdate {
    font-size: 19px;
    color: #222;
    margin-right: 25px;
  }
  .bottom-info {
    display: flex;
    align-items: center;
  }
  .reply-btn {
    width: 135px;
    height: 48px;
    line-height: 48px;
    font-size: 21px;
    color: #222;
  }
  /deep/.comment-item .like-btn .van-icon .active {
    color: red;
  }
  .like-btn {
    height: 30px;
    padding: 0;
    border: none;
    font-size: 19px;
    line-height: 30px;
    margin-right: 7px;
    .van-icon {
      font-size: 30px;
    }
  }
}
</style>
